<div class="container">
	<div class="row">
		<div class="col-md-12">
			<div class="titlepage">
				<h2>用户订单</h2>
			</div>
		</div>
	</div>
</div>
<div class="brand-bg">
	<div class="container">
		<div class="order-list">
			<div class="order-title">
				<a id="allorders" href="javascript:void(0);">全部订单</a>
				<a id="notpayorders" href="javascript:void(0);">未付款</a>
				<a id="payedorders" href="javascript:void(0);">已付款</a>
			</div>
			<div class="order-show">
				<table class="order-shuxing">
					<thead>
					<tr>
						<th>序号</th>
						<th>订单图片</th>
						<th>订单编号</th>
						<th>订单总价</th>
						<th>订单状态</th>
						<th>创建时间</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody>

					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(function (){
		doGetOrders(null);
		$("#notpayorders").click(function (){
			doGetOrders(0);
		});
		$("#payedorders").click(function (){
			doGetOrders(1);
		});
		$("#allorders").click(function (){
			doGetOrders(null);
		});
	});

	function doGetOrders(status){
		let url = `/ruoyi/order/getorder`;
		let param = {"status":status};
		$.ajax({
			url:url,
			data:param,
			dataType:"json",
			success(result){
				if (result.status===200){
					doLoadOrders(result.data);
					doLoadDelOrder();
					doLoadPay();
				}
			},
			error(){
				alert("获取失败!");
			}
		});
	}
	function doLoadPay(){
		$(".dopay").click(function (){
			let url = `/ruoyi/order/payorder`;
			let orderData = $(this).parents("tr").data("userorder");
			let params = {"orderId":orderData.orderId};
			$.ajax({
				url:url,
				data:params,
				dataType:"json",
				success(result){
					if (result.status===200){
						url = `/ruoyi/paysuccess`;
						jumphtml(url);
					}
				},
				error(){
					alert("支付失败");
				}
			});
		});
	}
	function doLoadDelOrder(){
		$(".delorder").click(function (){
			if (confirm("是否删除?")){
				let url = `/ruoyi/order/deleteorder`;
				let userorder = $(this).parents("tr").data("userorder");
				let params = {"orderId":userorder.orderId};
				$.ajax({
					url:url,
					data: params,
					dataType: "json",
					success(result) {
						if (result.status===200){
							doGetOrders(null);
							alert("删除成功!");
						}
					},
					error() {
						alert("删除失败!");
					}
				});
			}
		});
	}
	function doLoadOrders(orders){
		let tobody = $(".order-shuxing tbody");
		tobody.empty();
		for (let i=0;i<orders.length;i++){
			let tr = $("<tr></tr>");
			tr.append(doCreateOrders(i+1,orders[i]));
			tr.data("userorder",orders[i]);
			tobody.append(tr);
		}
	}
	function doCreateOrders(i,order){
		let orderstatus = order.status===0?"未付款":"已付款";
		let orderPayButton = order.status===0?`<a class="dopay" href="javascript:void(0);">支付</a>`:`<a>已付款</a>`;
		let ordercreated = new Date(order.created);
		let created = doStringtoDate(ordercreated);

		return `<td>${i}</td>
				<td><img src="${order.image}"></td>
				<td><span>${order.orderId}</span></td>
				<td><span>${order.total}</span></td>
				<td><span>${orderstatus}</span></td>
				<td><span>${created}</span></td>
				<td><a class="delorder" href="javascript:void(0);">删除</a>
				${orderPayButton}</td>`;
	}
</script>